// -----------------------------------------------------------------------------
//
// Work is licensed by Arthur Fermi (C)2008-2011,
// patches are licensed by Mirella Dallagio (C)2011
// under Creative Commons Attribution-Share Alike 3.0 Unported License
// To receive a copy of this license please visit
// http://creativecommons.org/licenses/by-sa/3.0/
//
// You are free to copy, distribute, transmit and adapt the work
// under the following conditions:
//
// You must attribute the work in the manner specified by the 
// author(s) or licensor (but not in any way that suggests that 
// they endorse you or your use of the work). 
//
// If you alter, transform, or build upon this work in any way,
// you may distribute the resulting work only under the same or 
// similar license to this one. 
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See points (6) and (7) of the license for more details.
//
// -----------------------------------------------------------------------------
//
// Changelog:
//
// zSN_SimNet_Event_Comms 1.01 - 2011-04-24 initial simnet release
// zSN_SimNet_Event_Comms 1.03 - 2011-04-26 fix for unban protocol mismatch
//
// -----------------------------------------------------------------------------
// Do not change anything below here or you will likely break something
// -----------------------------------------------------------------------------
// compile with lsl2
// -----------------------------------------------------------------------------

// SimNet - Event Communications

// URL Format
// http://<website>/simnet/simnet_eventpost.php?Date=<date/time yyyy-mm-dd hh:mm>&Sim=<sim name>&Staff=<Staff Name>&Type=<ban,unban,eject,etc>&Who=<Avatar Name>&WhoKey=<Avtar Key>&BanLen=<BanLen,might be NA or a space>&Reason=<Predefined Reason>&Comment=<Staff Comment>

// Global Variables
    list    glstEventReasons=["Other","Cancel","Text Spam","Particles","ExcessPrims","Assult","Harrasement","Copy Bot"];
    list    glstAgentCommands=["Banned","Unbanned","Ejected","Home"];
 
// Do not edit below this line or you might cause problems
    integer gintDCC;            // Dialog Communications Channel
    integer gintTempListen;     // Turn Listen on and off
    integer gintStep;           // 1 = Reason, 2 = Make Comment?, 3 = Enter Comment
    string  gstrURL;            // URL
    integer gintParcelComm1;      // Parcel Comm Channel 1
    string  gstrHTTP;

// Report
    list    glstEventReport;      // Report Data
    
    // Report List Order
    // Date/Time, Sim, Manager, Banned AV Key, Banned AV Name, Ban Length, Ban Reason, Ban Comment

funSubmitReport()
{
    // All work has been done, submit to the web.
    
    // Remove listeners
    llListenRemove(gintTempListen);
    
    // Remove Timers
    llSetTimerEvent(0);
    
    // URL Format
    // http://<website>/simnet/simnet_eventpost.php?Date=<date/time yyyy-mm-dd hh:mm>&Sim=<sim name>&Staff=<Staff Name>&Type=<ban,unban,eject,etc>&Who=<Avatar Name>&WhoKey=<Avtar Key>&BanLen=<BanLen,might be NA or a space>&Reason=<Predefined Reason>&Comment=<Staff Comment>

    
    // Build event repot URL
    string URL = gstrURL + "simnet_eventpost.php?Date=" + llList2String(glstEventReport, 0) + " &Sim=" + llEscapeURL(llList2String(glstEventReport, 1)) + "&Staff=" +  llEscapeURL(llList2String(glstEventReport, 2)) + "&Type =" + llEscapeURL(llList2String(glstEventReport, 3)) + "&Who=" + llEscapeURL(llList2String(glstEventReport, 5)) + "&WhoKey=" +  llEscapeURL(llList2String(glstEventReport, 4)) + "&BanLen=" + llEscapeURL(llList2String(glstEventReport, 6)) + "&Reason=" +  llEscapeURL(llList2String(glstEventReport, 7)) + "&Comment=" +  llEscapeURL(llList2String(glstEventReport, 8));

    // Post URL
    llHTTPRequest(URL,[HTTP_METHOD,"POST"],""); // Post to the web site
    
    // Notify User
    llOwnerSay("Event posted for " + llList2String(glstEventReport, 5) + ".  Thank you for your report.");
}                  

default
{
    state_entry()
    {
        // Setup random dialog channel
        gintDCC = llCeil(llFrand(10000.0));
        
        list lstTempList = llParseString2List(llGetObjectDesc(),["^"],[""]);
        
        // Set Parcel Comm Channels
        gintParcelComm1 = ((integer)llStringTrim((string)llList2List(lstTempList,1,1), STRING_TRIM) * 7) / 3; 

        // Get URL for web site
        llMessageLinked(LINK_SET, 333, "Getting URL", NULL_KEY);
   }
    
    link_message(integer sender_num, integer num, string str, key id)
    {
        if ( num == 334)
        {
            gstrURL = str;
        }
            
        else if (num == 666)
        {
        // Clean up ban report
        glstEventReport = [];

         // Convert message to list
        list lstTmp = llParseString2List(str, ["^"], [""]);

        // Find out what function was performed
        string strCommand = llList2String(lstTmp, 0);

        // If it needs land agents support, send them the command
        if (llListFindList(glstAgentCommands, [strCommand]) != -1)
        {
            // Send the command out to the parcel devices
            llRegionSay(gintParcelComm1, str);
        }
        
        
       // Date/Time Info
        integer t = (integer)llGetWallclock(); // seconds since midnight
        integer hours = t / 3600; // get hours (integer division chops off the decimals);
        integer minutes = (t % 3600) / 60; // divide by 60 because we want minutes
        integer seconds = t % 60; // get the seconds that didn't fit into full minutes
        // Add time to the Ban Report
        glstEventReport += (string)llGetDate() + "T" + (string)hours + ":" + (string)minutes + ":" + (string)seconds;
        
        // Add Sim
        glstEventReport += llGetRegionName();
        
        // Add managers name
        glstEventReport += llKey2Name(llGetOwner());
        
        // Add Event
        glstEventReport += llList2String(lstTmp, 0);

        // Person Banned Name
        glstEventReport +=llList2String(lstTmp, 1);
        
        // Add Banned Key
        glstEventReport += llList2String(lstTmp, 2);
        
        // Ban Length
        glstEventReport += llList2String(lstTmp, 3);
        
        // Get Event Reason
        // Set Listen
        gintTempListen = llListen(gintDCC, "","", "");

        // Send Dialog
        string strDialogText = "\nPlease indicate the reason for your event entry for " + llList2String(glstEventReport, 5) + ".  This information is to track events on a per-avatar basis and is very important.  If you don't answer 'unknown' wil lbe entered.  You will also get to leave a comment.";

        llDialog(llGetOwner(),strDialogText,glstEventReasons,gintDCC);
            
        // Set Step
        gintStep = 1;

        // Set Timout
        llSetTimerEvent(30);
        }
    } 
    
    listen(integer channel, string name, key id, string message)
    {
        if (channel == gintDCC)
        {
            if (llListFindList(glstEventReasons, [message]) >= 0)
            {
                // If its cancel, enter unknown, otherwise the button name is fine
                if (message == "Cancel")
                {
                    // Set Reason and Comment, then submit report
                    glstEventReport += "Unknown"; 
                    glstEventReport += "No Comment";
                    funSubmitReport();
                }
                else
                {
                    glstEventReport += message;
                }
                
                // Clear the timer
                llSetTimerEvent(0);
                
                // Send the Dialog
                llDialog(llGetOwner(), "Would you like to enter a comment about the event of " + llList2String(glstEventReport, 5) + "?", ["Comment","NoComment"],gintDCC);
                
                // Set Step
                gintStep = 2;
                
                // Set the timer
                llSetTimerEvent(30);
                
            }
            else if (message == "Comment")
            {
                // Clear the timer
                llSetTimerEvent(0);
                
                // Clear dailog listener
                llListenRemove(gintTempListen);

                // Set Listen in chat
                gintTempListen = llListen(9, "", llGetOwner(), ""); 
                
                // Send a Dialog box and a chat window stating what to do
                llDialog(llGetOwner(), "Please type in channel 9 (/9 comments) your comments about the event.  This will only record the first line you type in.  If you don't type anything in, or don't finish with in 60 seconds it will enter 'No Comment' and submit the report.",["Ok"], -991166);

                llOwnerSay("Please type in channel 9 (/9 comments) your comments about the event.  This will only record the first line you type in.  If you don't type anything in, or don't finish with in 60 seconds it will enter 'No Comment' and submit the report.");

                // Set Step
                gintStep = 3;

                // Set the timer
                llSetTimerEvent(60);
            }
            else if (message == "NoComment")
            {
                // Set No comment and submit report
                glstEventReport += "No Comment";
                funSubmitReport();  
            }
        }
        else if (channel == 9)
        {
            // Set the comment and submit the report
            glstEventReport += message;
            funSubmitReport();
        }
    }
    
    timer()
    {
        llListenRemove(gintTempListen);
        llSetTimerEvent(0);
        
        if (gintStep == 1)
        {
            // No response Set Reason and Comment, then submit report
            glstEventReport += "Unknown"; 
            glstEventReport += "No Comment";
            funSubmitReport();
        }
        else if (gintStep == 2)
        {
            // Set No comment and submit report
            glstEventReport += "No Comment";
            funSubmitReport();
        }
        else if (gintStep == 3)
        {
            // Set the comment and submit the report
            glstEventReport +=  "No Comment";
            funSubmitReport(); 
        }
    } 

    on_rez(integer start_param)
    {
        llResetScript();
    } 
    
}
